package org.rbreznak.tronco.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;

import org.rbreznak.tronco.sql.DatabaseHandler;

public abstract class Screen extends JPanel implements ActionListener, KeyListener, MouseListener{
	private Screen next = null;
	protected DatabaseHandler dbh;
	protected ArrayList<Screen> nodes = new ArrayList<Screen>();
	
    private List<ScreenListener> _listeners = new ArrayList<ScreenListener>();
	
	public abstract void decideNext();
	
	public abstract void reset();
	
	public void init () {
		
	}
	
	public Screen getNext() {
		decideNext();
		return next;
	}
	
	public void setNext(Screen next) {
		this.next = next;
	}
	
	public void addNode(Screen node, int index) {
		if(next == null) {
			next = node;
		}
		nodes.add(index, node);
	}
	
	public void addNode(Screen node) {
		if(next == null) {
			next = node;
		}
		nodes.add(node);
	}

    public synchronized void addScreenListener( ScreenListener l ) {
        _listeners.add( l );
    }
    
    public synchronized void removeScreenListener( ScreenListener l ) {
        _listeners.remove( l );
    }
    
    public synchronized void removeAllButOneScreenListener(ScreenListener l ) {
    	_listeners.clear();
    	addScreenListener(l);
    }
    
    protected synchronized void _fireScreenUpdate() {
    	EventObject evnt = new EventObject(this);
        Iterator listeners = _listeners.iterator();
        while( listeners.hasNext() ) {
            ( (ScreenListener) listeners.next() ).screenUpdate( evnt );
        }
    }
    
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public void actionPerformed(ActionEvent arg0) {

	}

	public void keyPressed(KeyEvent e) {	

	}

	public void keyReleased(KeyEvent e) {
		
	}

	public void keyTyped(KeyEvent e) {

	}
}
